<template>
  <div>
    <el-dialog :title="title" :visible="visible" :before-close="cloFn">
      <el-form ref="kehuForm" :model="form" :rules="rules">
        <el-form-item label="经纪人" prop="agent" :label-width="formLabelWidth">
          <el-select v-model="form.agent" placeholder="请选择经纪人">
            <el-option
              v-for="item in select"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="姓  名" prop="name" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="邮  箱" prop="email" :label-width="formLabelWidth">
          <el-input v-model="form.email" autocomplete="off" />
        </el-form-item>
        <el-form-item label="电  话" prop="phone" :label-width="formLabelWidth">
          <el-input v-model="form.phone" autocomplete="off" />
        </el-form-item>
        <el-form-item label="微  信" prop="wechat" :label-width="formLabelWidth">
          <el-input v-model="form.wechat" autocomplete="off" />
        </el-form-item>
        <el-form-item label="描  述" prop="desc" :label-width="formLabelWidth">
          <el-input
            v-model="form.desc"
            type="textarea"
            :rows="3"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="封  面" :label-width="formLabelWidth">
          <ImageUpload ref="imageupload" :staff-photo.sync="form.image" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cloFn">取 消</el-button>
        <el-button type="primary" @click="butOk">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addKehu, seeKehuById, editKehu } from '@/api/kehu'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    select: {
      type: Array,
      default() {
        return []
      }
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      form: {
        id: ''
      },
      formLabelWidth: '120px',
      rules: {
        // agent: [{ required: true, message: '经纪人不能为空', trigger: 'change' }],
        name: [
          { required: true, message: '请输入客户名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        desc: [{ required: true, message: '描述不能为空', trigger: 'change' }]

      }
    }
  },
  created() {

  },
  mounted() {

  },
  methods: {
    cloFn() {
      this.$emit('update:visible', false)
      this.$refs.imageupload.fileList = []
      this.$refs.kehuForm.resetFields()
    },
    butOk() {
      this.$refs.kehuForm.validate(async(isOk) => {
        if (isOk) {
          console.log('校验通过')
          if (this.title === '添加客户') {
            console.log('添加客户')
            const res = await addKehu(this.form)
            console.log('添加客户', res)
            this.$emit('getKehuInfoFn')
          } else if (this.title === '编辑客户') {
            const res = await editKehu(this.form)
            console.log('编辑客户', res)
            this.$emit('getKehuInfoFn')
            this.$message.success('编辑成功')
          }
          this.cloFn()
        }
      })
    },
    async seeKehuByIdFn(id) {
      const res = await seeKehuById(id)
      console.log('编辑回显', res)
      this.form = res
      this.$refs.imageupload.fileList.push({
        url: this.form.image
      })
    }

  }
}
</script>

<style lang='scss' scoped >
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
